package com.zhuzg.controller;

import com.zhuzg.domain.AjaxResult;
import com.zhuzg.exception.MesException;
import com.zhuzg.pojo.request.StorageInRequestDto;
import com.zhuzg.pojo.request.StorageRequestDto;
import com.zhuzg.pojo.response.StorageResponseDto;
import com.zhuzg.service.StorageService;
import com.zhuzg.vlidat.GroupAdd;
import com.zhuzg.vlidat.GroupUpdate;
import org.hibernate.validator.internal.engine.groups.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.ConvertGroup;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 仓库管理的controller
 */
@RestController
public class StorageController {



    @Autowired
    StorageService storageService;


    @RequestMapping("test")
    public AjaxResult<String> test(){
        AjaxResult result = AjaxResult.Ok(storageService.testException());
        System.out.println("执行到test");
        return result;
    }

    @RequestMapping("list")
    public AjaxResult<List<StorageResponseDto>> list(StorageRequestDto condin){
        return AjaxResult.Ok(storageService.query(condin));
    }

    @RequestMapping("add")
    public AjaxResult add(@Validated(value = GroupAdd.class)   @RequestBody StorageInRequestDto storageInRequestDto){
        if (null==storageInRequestDto.getStorageId() && 0==storageInRequestDto.getStorageId()){
            throw  new MesException("仓库的id 不能为空");
        }
        storageService.add(storageInRequestDto);
        return AjaxResult.Ok("ok");
    }

    @RequestMapping("update")
    public AjaxResult update(@Validated(value = GroupUpdate.class) @RequestBody StorageInRequestDto storageInRequestDto){
        storageService.update(storageInRequestDto);
        return  AjaxResult.Ok("ok");
    }

    /**
     * 显示pdf
     */
    @GetMapping("/preview")
    public ResponseEntity<Resource> previewFile() throws IOException {
        String filePath = "D:\\2112JavaA\\课堂笔记\\SpringBootAdmin的笔记.pdf"; // 替换为实际文件路径

        File file = new File(filePath);
        InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

        HttpHeaders  httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_PDF);
        // inline 是预览  attachment 是下载
        httpHeaders.setContentDisposition(ContentDisposition.builder("inline").build());

        return ResponseEntity.ok()
                .contentLength(file.length())
                .contentType(MediaType.APPLICATION_PDF)
                .headers(httpHeaders)
               // .header(HttpHeaders.CONTENT_DISPOSITION,
               //         "attachment;filename=" + URLEncoder.encode("下载.pdf","utf-8"))

                .body(resource);
    }



    @RequestMapping("findstorage")

    public AjaxResult<StorageResponseDto> findById(Integer id){
        StorageResponseDto dto = storageService.getById(id);
        return AjaxResult.Ok(dto);

    }

    /**
     * 删除
     * @param idList
     * @return
     */
    @RequestMapping("del")
    public AjaxResult del(List<Integer> idList){
        storageService.delete(idList);
        return AjaxResult.Ok("ok");
    }



}
